﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Peter.Common.MainMenu;
using Peter.Common.Utilities;
using Peter.Ftp.Logic;

namespace Peter.Ftp.Tests.Logic
{
   /// <summary>
   /// Test cases for the option ftp model.
   /// </summary>
   [TestClass]
   public class OptionsFtpTests
   {
      private bool m_PropertyChanged;

      /// <summary>
      /// Tests the constructor.
      /// </summary>
      [TestMethod]
      public void TestConstructor ()
      {
         var model = new OptionsFtp ();
         Assert.IsNotNull (model.AddConnection);
         Assert.IsNotNull (model.FtpConnections);
         Assert.IsNotNull (model.RemoveConnection);
         Assert.AreEqual (OptionsFtp.PAGE_NAME, model.Name);
         Assert.AreEqual (MainMenuIcon.Ftp, model.OptionPageIcon);
         Assert.IsTrue (ViewRegistry.Contains (typeof (OptionsFtp)));
         Assert.IsFalse (model.UploadOnSave);

         model.PropertyChanged += this.OnPropertyChanged;
         this.m_PropertyChanged = false;
         model.UploadOnSave = true;
         Assert.IsTrue (this.m_PropertyChanged);
         Assert.IsTrue (model.UploadOnSave);
         model.PropertyChanged -= this.OnPropertyChanged;
      }

      /// <summary>
      /// Occurs when a property changes.
      /// </summary>
      /// <param name="sender">Object that raised the event.</param>
      /// <param name="e">PropertyChangedEventArgs</param>
      private void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
      {
         this.m_PropertyChanged = true;
      }

      /// <summary>
      /// Tests the add connection.
      /// </summary>
      [TestMethod]
      public void TestAddConnection ()
      {
         var model = new OptionsFtp ();
         Assert.IsTrue (model.AddConnection.CanExecute (null));
         model.AddConnection.Execute (null);
         Assert.AreEqual (1, model.FtpConnections.Count);
         Assert.IsNotNull (model.SelectedConnection);
         Assert.AreEqual ("New Ftp Connection", model.SelectedConnection.Name);
      }

      /// <summary>
      /// Tests the remove connection.
      /// </summary>
      [TestMethod]
      public void TestRemoveConnection ()
      {
         var model = new OptionsFtp ();
         Assert.IsFalse (model.RemoveConnection.CanExecute (null));
         model.AddConnection.Execute (null);
         Assert.IsNotNull (model.SelectedConnection);
         Assert.IsTrue (model.RemoveConnection.CanExecute (null));
         Assert.AreEqual (1, model.FtpConnections.Count);
         model.RemoveConnection.Execute (null);
         Assert.AreEqual (0, model.FtpConnections.Count);
      }

      /// <summary>
      /// Tests the save and load feature.
      /// </summary>
      [TestMethod]
      public void TestSaveLoad ()
      {
         var model = new OptionsFtp { UploadOnSave = true };
         model.AddConnection.Execute (null);
         var savedData = model.SaveToXml ();
         model.LoadFromXml (string.Empty);
         model.LoadFromXml ("<badxml><tag</bd>");

         var model2 = new OptionsFtp ();
         model2.LoadFromXml (savedData);
         Assert.IsTrue (model2.UploadOnSave);
         Assert.AreEqual (1, model2.FtpConnections.Count);
      }
   }
}
